package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    AdSensitiveMapper adSensitiveMapper;

    /**
     * 分页查询敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult querySensitiveList(SensitiveDTO dto) {
        //1.检查参数
        dto.checkParam();
        //2.分页查询通过名称
        Page<AdSensitive> pageReq = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        IPage<AdSensitive> pageResult = this.page(pageReq, queryWrapper);
        //3.结果返回
        return new PageResponseResult(
                dto.getPage(),
                dto.getSize(),
                pageResult.getTotal(),
                pageResult.getRecords());
    }

    /**
     * 新增敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insertSensitiveList(AdSensitive adSensitive) {
        //1.检查参数
        String adSensitives = adSensitive.getSensitives();
        if (adSensitive == null || StringUtils.isBlank(adSensitives)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "这是一个无效的参数！");
        }
        //2. 判断是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitives));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "新增的数据已经存在！");
        }
        //3.保存到数据库
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        //4.返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitiveList(AdSensitive adSensitive) {
        //1.检查参数
        Integer id = adSensitive.getId();
        String sensitives = adSensitive.getSensitives();
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "此ID无效！");
        }
        //2. 判断是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, sensitives));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "名称已经存在！");
        }
        //3.修改
        this.updateById(adSensitive);
        //4.返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitiveListById(Integer id) {
        //1.检查参数
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询敏感词是否存在
        AdSensitive adSensitive = getById(id);
        if (adSensitive==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.删除
        this.removeById(id);
        //4.返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
